﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace 电子书处理小程序
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            lvFile.View = View.List;
        }

        /// <summary>
        /// add file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog f = new OpenFileDialog();
            f.Multiselect = true;
            if (f.ShowDialog(this) == DialogResult.OK)
            {
                foreach (string file in f.FileNames)
                {
                    ListViewItem lvitem = new ListViewItem();
                    lvitem.Tag = file;
                    lvitem.Text =Path.GetFileName(file);
                    lvFile.Items.Add(lvitem);
                }
            }
        }

        /// <summary>
        /// clear Empty line
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in lvFile.SelectedItems)
            {              
                string fileName = item.Tag.ToString();
                string newFileName=Path.Combine(Path.GetDirectoryName(fileName),"newFile"+Path.GetExtension(fileName));
                FileStream fsNew = File.Create(newFileName);
                StreamReader sr = new StreamReader(fileName, Encoding.Default);
                StreamWriter sw = new StreamWriter(fsNew,Encoding.Default);
                while (!sr.EndOfStream)
                {
                    string lineContent = sr.ReadLine();
                    if (!string.IsNullOrEmpty(lineContent))
                    {
                        sw.WriteLine(lineContent);
                    }
                }
                sr.Close();
                sw.Close();
                fsNew.Close();
            }
            MessageBox.Show("完成！");
        }

        /// <summary>
        /// clear file list
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            lvFile.Items.Clear();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string newFileName = Path.Combine(Path.GetDirectoryName(lvFile.SelectedItems[0].Tag.ToString()), "newFile" + Path.GetExtension(lvFile.SelectedItems[0].Tag.ToString()));
            FileStream fsNew = File.Create(newFileName);
            StreamWriter sw = new StreamWriter(fsNew, Encoding.Default);

            foreach (ListViewItem item in lvFile.SelectedItems)
            {
                string fileName = item.Tag.ToString();
                StreamReader sr = new StreamReader(fileName, Encoding.Default);

                string lineContent = sr.ReadToEnd();//.ReadLine();
                if (!string.IsNullOrEmpty(lineContent))
                {
                    sw.WriteLine(lineContent);
                }
                sw.WriteLine("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
                sr.Close();
            }
            sw.Close();
            fsNew.Close();
            MessageBox.Show("完成！");
        }

        /// <summary>
        /// 打开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {

        }



    }
}